{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"tags\": [{\"name\": \"building\", \"confidence\": 0.9925744533538818}, {\"name\": \"outdoor\", \"confidence\": 0.9722763895988464}, {\"name\": \"street\", \"confidence\": 0.8764569759368896}, {\"name\": \"city\", \"confidence\": 0.7708874344825745}, {\"name\": \"people\", \"confidence\": 0.6341404914855957}, {\"name\": \"busy\", \"confidence\": 0.36196476221084595}, {\"name\": \"night\", \"confidence\": 0.33305415511131287}, {\"name\": \"ride\", \"confidence\": 0.15155000984668732}, {\"name\": \"crowd\", \"confidence\": 0.020897958427667618}], \"requestId\": \"c3a89ee9-3681-44ea-acae-6f44dee6d77d\", \"metadata\": {\"width\": 450, \"height\": 600, \"format\": \"Jpeg\"}}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import matplotlib.pyplot as plt\n",
    "import json\n",
    "from PIL import Image\n",
    "from io import BytesIO\n",
    "\n",
    "# Replace <Subscription Key> with your valid subscription key.\n",
    "subscription_key = \"my_key\"\n",
    "assert subscription_key\n",
    "\n",
    "# You must use the same region in your REST call as you used to get your\n",
    "# subscription keys. For example, if you got your subscription keys from\n",
    "# westus, replace \"westcentralus\" in the URI below with \"westus\".\n",
    "#\n",
    "# Free trial subscription keys are generated in the \"westus\" region.\n",
    "# If you use a free trial subscription key, you shouldn't need to change\n",
    "# this region.\n",
    "vision_base_url = \"http://api.cognitive.azure.cn/vision/v1.0/\"\n",
    "\n",
    "analyze_url = vision_base_url + \"tag\"\n",
    "\n",
    "# Set image_url to the URL of an image that you want to analyze.\n",
    "image_url = \"https://upload.wikimedia.org/wikipedia/commons/thumb/1/12/\" + \\\n",
    "    \"Broadway_and_Times_Square_by_night.jpg/450px-Broadway_and_Times_Square_by_night.jpg\"\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key }\n",
    "params  = {'visualFeatures': 'Categories,Description,Color'}\n",
    "data    = {'url': image_url}\n",
    "response = requests.post(analyze_url, headers=headers, params=params, json=data)\n",
    "response.raise_for_status()\n",
    "\n",
    "analysis = response.json()\n",
    "data = json.dumps(response.json())\n",
    "print(data)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"metadata\":{\"format\":\"Jpeg\",\"height\":600,\"width\":450},\"requestId\":\"0b7a20d4-8a95-4ea8-a730-1704f99db379\",\"tags\":[{\"confidence\":0.9925744533538818,\"name\":\"building\"},{\"confidence\":0.9722763895988464,\"name\":\"outdoor\"},{\"confidence\":0.8764570355415344,\"name\":\"street\"},{\"confidence\":0.7708874940872192,\"name\":\"city\"},{\"confidence\":0.6341403722763062,\"name\":\"people\"},{\"confidence\":0.3619648516178131,\"name\":\"busy\"},{\"confidence\":0.33305415511131287,\"name\":\"night\"},{\"confidence\":0.15155011415481567,\"name\":\"ride\"},{\"confidence\":0.020897958427667618,\"name\":\"crowd\"}]}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'metadata': {'format': 'Jpeg', 'height': 600, 'width': 450},\n",
       " 'requestId': '0b7a20d4-8a95-4ea8-a730-1704f99db379',\n",
       " 'tags': [{'confidence': 0.9925744533538818, 'name': 'building'},\n",
       "  {'confidence': 0.9722763895988464, 'name': 'outdoor'},\n",
       "  {'confidence': 0.8764570355415344, 'name': 'street'},\n",
       "  {'confidence': 0.7708874940872192, 'name': 'city'},\n",
       "  {'confidence': 0.6341403722763062, 'name': 'people'},\n",
       "  {'confidence': 0.3619648516178131, 'name': 'busy'},\n",
       "  {'confidence': Decimal('0.33305415511131287'), 'name': 'night'},\n",
       "  {'confidence': Decimal('0.15155011415481567'), 'name': 'ride'},\n",
       "  {'confidence': Decimal('0.020897958427667618'), 'name': 'crowd'}]}"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import demjson\n",
    "data = demjson.encode(response.json())\n",
    "print(data)\n",
    "text = demjson.decode(data)\n",
    "text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>confidence</th>\n",
       "      <th>name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.992574</td>\n",
       "      <td>building</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.972276</td>\n",
       "      <td>outdoor</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.876457</td>\n",
       "      <td>street</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.770887</td>\n",
       "      <td>city</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.63414</td>\n",
       "      <td>people</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.361965</td>\n",
       "      <td>busy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.33305415511131287</td>\n",
       "      <td>night</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.15155011415481567</td>\n",
       "      <td>ride</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.020897958427667618</td>\n",
       "      <td>crowd</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             confidence      name\n",
       "0              0.992574  building\n",
       "1              0.972276   outdoor\n",
       "2              0.876457    street\n",
       "3              0.770887      city\n",
       "4               0.63414    people\n",
       "5              0.361965      busy\n",
       "6   0.33305415511131287     night\n",
       "7   0.15155011415481567      ride\n",
       "8  0.020897958427667618     crowd"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# data['pois']是列表，含关键字搜索结果\n",
    "# 大数据模块 pandas 简化输出 \n",
    "import pandas as pd \n",
    "df = pd.DataFrame(text['tags'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    building\n",
       "1     outdoor\n",
       "2      street\n",
       "3        city\n",
       "4      people\n",
       "5        busy\n",
       "6       night\n",
       "7        ride\n",
       "8       crowd\n",
       "Name: name, dtype: object"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['building',\n",
       " 'outdoor',\n",
       " 'street',\n",
       " 'city',\n",
       " 'people',\n",
       " 'busy',\n",
       " 'night',\n",
       " 'ride',\n",
       " 'crowd']"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#列表推倒\n",
    "name = [x['name'] for x in text['tags']]\n",
    "name"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
